package com.ailiwu.www.ailiwu.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ailiwu.www.ailiwu.db.SQLHelper;

/**
 * Author:    XiYoung
 * Version    V1.0
 * Date:      2015/12/5 000515:46
 * Description:
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * 2015/12/5 0005       XiYoung              1.0                   1.0
 * Why & What is modified:
 */
public class DBUtil {
    private static DBUtil mInstance;
    private Context mContext;
    private SQLHelper mSQLHelp;
    private SQLiteDatabase mSQLiteDatabase;

    private DBUtil(Context context) {
        mContext = context;
        mSQLHelp = new SQLHelper(context);
        mSQLiteDatabase = mSQLHelp.getWritableDatabase();
    }
    /**
     * 初始化数据库操作DBUtil类
     */
    public static DBUtil getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new DBUtil(context);
        }
        return mInstance;
    }
    /**
     * 关闭数据库
     */
    public void close() {
        mSQLHelp.close();
        mSQLHelp = null;
        mSQLiteDatabase.close();
        mSQLiteDatabase = null;
        mInstance = null;
    }

    /**
     * 添加数据
     */
    public void insertData(ContentValues values) {
        mSQLiteDatabase.insert(SQLHelper.TABLE_CHANNEL, null, values);
    }

    /**
     * 更新数据
     *
     * @param values
     * @param whereClause
     * @param whereArgs
     */
    public void updateData(ContentValues values, String whereClause,
                           String[] whereArgs) {
        mSQLiteDatabase.update(SQLHelper.TABLE_CHANNEL, values, whereClause,
                whereArgs);
    }

    /**
     * 删除数据
     *
     * @param whereClause
     * @param whereArgs
     */
    public void deleteData(String whereClause, String[] whereArgs) {
        mSQLiteDatabase
                .delete(SQLHelper.TABLE_CHANNEL, whereClause, whereArgs);
    }

    /**
     * 查询数据
     *
     * @param columns
     * @param selection
     * @param selectionArgs
     * @param groupBy
     * @param having
     * @param orderBy
     * @return
     */
    public Cursor selectData(String[] columns, String selection,
                             String[] selectionArgs, String groupBy, String having,
                             String orderBy) {
        Cursor cursor = mSQLiteDatabase.query(SQLHelper.TABLE_CHANNEL,columns, selection, selectionArgs, groupBy, having, orderBy);
        return cursor;
    }
}
